--
-- Author: mm
-- Date: 2018-04-17 15:06:49
--
-- HandbookController
-- 指南相关控制管理器

local HandbookView = import("..views.HandbookView")
local ItemGotWayView = import("..views.ItemGotWayView")
local HandbookTipsPanel = import("..views.HandbookTipsPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

local string_split = string.split

function ClassRef:ctor(data)
    self.super.ctor(self, data)
    
end

function ClassRef:init()
    self._model = app:getInst("HandbookModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end


function ClassRef:openView(data)
    -- self._model:_parseTargetsList() -- 展开UI的时候重新解析一次配置表

    if cc.isDead(self._view) then
        self._view = HandbookView.new( app.mvc.VIEWTYPE_BLUR )
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view)
    end
    self._view:reloadMenu(data, self._redPointData)
    self._view:show()
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end

-- 以下：物品获取途径view
function ClassRef:openGotWayView(data)
    local view = ItemGotWayView.new()
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    -- view:onOperate(handler(self, self.onOperate))
    view:refreshContent( data )
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end
-------------------------------------------------------
function ClassRef:onOperate(op, data)
    --dump(op)
    if op == "getChallengeLVInfo" then
        self._model:reqGetDirectoryChallengeInfo()
    elseif op == "startGetChallengeAward" then
        self._model:reqBuyDirectoryChallengeAward()
    elseif op == "showDirectoryChallengeTips" then
        self:showDirectoryChallengeTips(data, L("directory_challenge_tips1"),L("directory_challenge_tips2"),L("directory_challenge_tips3"))
    end
end

function ClassRef:refreshDirectoryChallengePanel(data)
    if cc.isAlive(self._view) then
        self._view:refreshDirectoryChallengePanel(data)
    end
end

--显示挑战tips
function ClassRef:showDirectoryChallengeTips(pos,title,str1,str2)
    if cc.isDead( self._directoryChallengeTips ) then
        self._directoryChallengeTips = HandbookTipsPanel.create()

        self._view:addChild( self._directoryChallengeTips )
        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(self._directoryChallengeTips, function()
            if self._directoryChallengeTips then
                self._directoryChallengeTips:removeFromParent(true)
                self._directoryChallengeTips = nil
            end
        end)
    end

    self._directoryChallengeTips:setContentText(title,str1,str2)
    self._directoryChallengeTips:setVisible( true )

    local width = self._directoryChallengeTips:getContentSize2()
    local posX = pos.x
    if posX + width > display.width then
        posX = display.width - width
    end
    self._directoryChallengeTips:setPosition( cc.p( posX, pos.y ) )
end

function ClassRef:setRedPoint(id, isRed)
    if not self._redPointData then
        self._redPointData = {}
    end

    if isRed then
        self._redPointData[tostring(id)] = true
    else
        self._redPointData[tostring(id)] = nil
    end
    UD:updateWelfareNewPoint(Constant.FUNC_TYPE_ACTIVITY, table.nums(self._redPointData) > 0)
    UD:updateWelfareNewPoint(Constant.FUNC_TYPE_GUIDE_STRENGTHEN, table.nums(self._redPointData) > 0)
end


--更新指南上的红点
function ClassRef:setDirectoryChallengeRedPoint(isRed)
    self:setRedPoint(Constant.DirectoryChallengeConst, isRed)

    if cc.isAlive(self._view) then
        self._view:reloadMenu(self._view:getCurSelectedSubKey(),self._redPointData)
    end
end
return ClassRef
